package org.jeecg.modules.checkInventoryPt.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.checkInventory.entity.CheckInventory;
import org.jeecg.modules.checkInventoryPt.entity.CheckInventoryPt;
import org.jeecg.modules.checkInventoryPt.service.ICheckInventoryPtService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.dataLog.entity.DataLog;
import org.jeecg.modules.dataLog.service.IDataLogService;
import org.jeecg.modules.operationLog.entity.OperationLog;
import org.jeecg.modules.operationLog.service.IOperationLogService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: pt盘点表
 * @Author: jeecg-boot
 * @Date: 2024-07-24
 * @Version: V1.0
 */
@Api(tags = "pt盘点表")
@RestController
@RequestMapping("/checkInventoryPt/checkInventoryPt")
@Slf4j
public class CheckInventoryPtController extends JeecgController<CheckInventoryPt, ICheckInventoryPtService> {
    @Autowired
    private ICheckInventoryPtService checkInventoryPtService;
    @Autowired
    private IOperationLogService operationLogService;
    @Autowired
    private IDataLogService dataLogService;

    /**
     * 分页列表查询
     *
     * @param checkInventoryPt
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "pt盘点表-分页列表查询")
    @ApiOperation(value = "pt盘点表-分页列表查询", notes = "pt盘点表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CheckInventoryPt checkInventoryPt,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CheckInventoryPt> queryWrapper = QueryGenerator.initQueryWrapper(checkInventoryPt, req.getParameterMap());
        Page<CheckInventoryPt> page = new Page<CheckInventoryPt>(pageNo, pageSize);
        IPage<CheckInventoryPt> pageList = checkInventoryPtService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @PostMapping("/handleCheck")
    public Result<?> handleCheck(@RequestBody CheckInventoryPt checkInventoryPt) {
        int number = checkInventoryPt.getNumber();
        int checkNumber = checkInventoryPt.getCheckNumber();
        int differentNumber = checkNumber - number;
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        checkInventoryPt.setDifferentNumber(differentNumber)
                .setCheckMan(user.getRealname()).setCheckTime(new Date());
        if (differentNumber == 0) {
            checkInventoryPt.setResult(0);
            checkInventoryPt.setStatus(4);
        } else if (differentNumber < 0) {
            checkInventoryPt.setResult(-1);
            checkInventoryPt.setStatus(2);
        } else {
            checkInventoryPt.setResult(1);
            checkInventoryPt.setStatus(2);
        }
        OperationLog operationLog = new OperationLog();
        operationLog.setSkuCode(checkInventoryPt.getSkuCode()).setOperationTime(new Date())
                .setOperationType("盘点操作").setOperator(user.getRealname())
                .setLocation(checkInventoryPt.getLocation());
        checkInventoryPtService.updateById(checkInventoryPt);
        operationLogService.save(operationLog);
        return Result.OK("盘点成功", null);
    }

    @PostMapping("/sycn")
    public Result<?> sycn(@RequestBody CheckInventoryPt checkInventoryPt) {
        CheckInventoryPt byId = checkInventoryPtService.getById(checkInventoryPt.getId());
        if (byId == null) {
            return Result.error("未找到对应数据");
        }
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        OperationLog operationLog = new OperationLog();
        operationLog.setSkuCode(checkInventoryPt.getSkuCode()).setOperationTime(new Date())
                .setOperationType("处理盘点差异").setOperator(user.getRealname())
                .setLocation(checkInventoryPt.getLocation());
        checkInventoryPt.setStatus(3);
        checkInventoryPtService.updateById(checkInventoryPt);
        operationLogService.save(operationLog);
        return Result.OK("差异处理成功", null);
    }

    /**
     *   添加
     *
     * @param checkInventoryPt
     * @return
     */
    @AutoLog(value = "pt盘点表-添加")
    @ApiOperation(value = "pt盘点表-添加", notes = "pt盘点表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CheckInventoryPt checkInventoryPt) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        DataLog dataLog = new DataLog();
        dataLog.setOperator(user.getRealname()).setOperationTime(new Date())
                .setOperationType("新增").setOperationClass("盘点表")
                .setUpdated(checkInventoryPt.toString());
        dataLogService.save(dataLog);
        checkInventoryPtService.save(checkInventoryPt);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param checkInventoryPt
     * @return
     */
    @AutoLog(value = "pt盘点表-编辑")
    @ApiOperation(value = "pt盘点表-编辑", notes = "pt盘点表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CheckInventoryPt checkInventoryPt) {
        CheckInventoryPt byId = checkInventoryPtService.getById(checkInventoryPt.getId());
        if (byId == null) {
            return Result.error("未找到对应数据");
        }
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        DataLog dataLog = new DataLog();
        dataLog.setOperator(user.getRealname()).setOperationTime(new Date())
                .setOperationType("编辑").setOperationClass("盘点表")
                .setOriginal(byId.toString()).setUpdated(checkInventoryPt.toString());
        dataLogService.save(dataLog);
        checkInventoryPtService.updateById(checkInventoryPt);
        return Result.OK("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "pt盘点表-通过id删除")
    @ApiOperation(value = "pt盘点表-通过id删除", notes = "pt盘点表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        CheckInventoryPt byId = checkInventoryPtService.getById(id);
        if (byId == null) {
            return Result.error("未找到对应数据");
        }
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        DataLog dataLog = new DataLog();
        dataLog.setOperator(user.getRealname()).setOperationTime(new Date())
                .setOperationType("删除").setOperationClass("盘点表")
                .setOriginal(byId.toString());
        dataLogService.save(dataLog);
        checkInventoryPtService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "pt盘点表-批量删除")
    @ApiOperation(value = "pt盘点表-批量删除", notes = "pt盘点表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        List<DataLog> dataLogs = new ArrayList<>();
        for (CheckInventoryPt checkInventory : checkInventoryPtService.listByIds(list)) {
            DataLog dataLog = new DataLog();
            dataLog.setOperator(user.getRealname()).setOperationTime(date)
                    .setOperationType("删除").setOperationClass("库存表")
                    .setOriginal(checkInventory.toString());
            dataLogs.add(dataLog);
        }
        dataLogService.saveBatch(dataLogs);
        this.checkInventoryPtService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "pt盘点表-通过id查询")
    @ApiOperation(value = "pt盘点表-通过id查询", notes = "pt盘点表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CheckInventoryPt checkInventoryPt = checkInventoryPtService.getById(id);
        if (checkInventoryPt == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(checkInventoryPt);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param checkInventoryPt
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CheckInventoryPt checkInventoryPt) {
        return super.exportXls(request, checkInventoryPt, CheckInventoryPt.class, "pt盘点表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CheckInventoryPt.class);
    }

}
